package cn.ffcs.uec.view.action;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uec.view.action.bean.UecImportantWorkEditBean;
import cn.ffcs.uec.view.manager.UecImportantWorkManager;
import cn.ffcs.uec.view.model.UecImportantWork;
import cn.ffcs.uec.view.model.UecStaffAll;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecImportantWorkEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecImportantWorkEditBean bean = new UecImportantWorkEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecStaffAll uecStaffAll;
	//方便人员转换
	private StaffAndOutStaff staffAndOutStaff = new StaffAndOutStaff();
	private UecImportantWork uecImportant = new UecImportantWork();
	private UecImportantWorkManager uecImportantWorkManager = (UecImportantWorkManager) ApplicationContextUtil
			.getBean("uecImportantWorkManager");
  int countImportantB=0;
  int countImportantJ=0;
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecImportantWorkEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		// //项目角色
		List<NodeVo> nodeVoIshiskeypsnList = new ArrayList<NodeVo>();
		String[][] ishiskeypsn = { { "组长", "组长" }, { "副组长", "副组长" }, { "组员", "组员" } };
		for (int i = 0; i < ishiskeypsn.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(ishiskeypsn[i][j]);
				nodeVo.setName(ishiskeypsn[i][j + 1]);
			}
			nodeVoIshiskeypsnList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getRoleProjectName(), nodeVoIshiskeypsnList);

		// 制定方案角色(牵头负责、主要参与者、配合者)
		
		List<NodeVo> nodeVoIshiskeypsnList1 = new ArrayList<NodeVo>();
		String[][] ishiskeypsn1 = { { "牵头负责", "牵头负责" }, { "主要参与者", "主要参与者" }, { "配合者", "配合者" } };
		for (int i = 0; i < ishiskeypsn1.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(ishiskeypsn1[i][j]);
				nodeVo.setName(ishiskeypsn1[i][j + 1]);
			}
			nodeVoIshiskeypsnList1.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getRoleMakeName(), nodeVoIshiskeypsnList1);
		// 参与范围
		List<NodeVo> nodeVoIshiskeypsnList2 = new ArrayList<NodeVo>();
		String[][] ishiskeypsn2 = { { "本单位", "本单位" }, { "集团", "集团" }};
		for (int i = 0; i < ishiskeypsn2.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(ishiskeypsn2[i][j]);
				nodeVo.setName(ishiskeypsn2[i][j + 1]);
			}
			nodeVoIshiskeypsnList2.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getJointype(), nodeVoIshiskeypsnList2);
	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		uecStaffAll = (UecStaffAll) arg.get("uecStaffAll");
		if ("add".equals(opType)) {
			this.bean.getUecImportantWorkEditWindow().setTitle("新增专业工作情况");
			bean.getUecImportantWorkEditDiv().setHeight("40%");
		}
		if ("view".equals(opType)) {
			this.bean.getUecImportantWorkEditWindow().setTitle("查看奖励信息");
			uecImportant = (UecImportantWork) arg.get("uecImportant");
			if (null != uecImportant) {
				//PubUtil.fillBeanFromPo(uecEnc, bean);
				bean.getButtonDiv().setVisible(false);
				PubUtil.readOnlyComponent(bean.getUecImportantWorkEditDiv(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecImportantWorkEditWindow().setTitle("编辑专业工作情况");
			bean.getUecImportantWorkEditDiv().setHeight("40%");
			uecImportant = (UecImportantWork) arg.get("uecImportant");
			if (null != uecImportant) {
				PubUtil.fillBeanFromPo(uecImportant, bean);
			}
		}
		staffAndOutStaff.setPkPsndoc(uecStaffAll.getPkpsndoc());
		List<UecImportantWork> findUecImportantByPkPsndoc = uecImportantWorkManager.findUecImportantByPkPsndoc(staffAndOutStaff,"");
		if(findUecImportantByPkPsndoc.size()>0) {
			for(UecImportantWork UecImportantWork:findUecImportantByPkPsndoc) {
				if("本单位".equals(UecImportantWork.getJointype())) {
					countImportantB=countImportantB+1;
				}
				if("集团".equals(UecImportantWork.getJointype())) {
					countImportantJ=countImportantJ+1;
				}
			}
		}
		
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		// 获取登录用户
		//String currentName = PlatformUtil.getCurrentUser().getScreenName();
		if ("add".equals(opType)) {
			uecImportant =new UecImportantWork();
			//PubUtil.fillPoFromBean(bean, uecImportant);
			uecImportant.setPkPsndoc(uecStaffAll.getPkpsndoc());
			if(!"无".equals(bean.getWorkName().getValue().trim())) {
				uecImportant.setContextName(bean.getContextName().getValue());
				uecImportant.setJoindate(bean.getJoindate().getValue());
				uecImportant.setRoleMakeName((String)bean.getRoleMakeName().getSelectedItem().getValue());
				uecImportant.setRoleProjectName((String)bean.getRoleProjectName().getSelectedItem().getValue());
				uecImportant.setWorkResult(bean.getWorkResult().getValue());
				if("本单位".equals((String)bean.getJointype().getSelectedItem().getValue())) {
					if(countImportantB>=5) {
						Messagebox.show("您已维护 单位性质为本单位的5条记录，不可再次进行新增!");
						return;
					}
				}
				if("集团".equals((String)bean.getJointype().getSelectedItem().getValue())) {
					if(countImportantJ>=5) {
						Messagebox.show("您已维护 单位性质为集团的5条记录，不可再次进行新增!");
						return;
					}
				}
			}
			uecImportant.setJointype((String)bean.getJointype().getSelectedItem().getValue());
			uecImportant.setWorkName(bean.getWorkName().getValue());
			uecImportant.setStatuscd("1000");
			// 创建时间
			uecImportant.setCreatedate(new Date());//转换结果为yyyy-mm-dd 00:00:00
			uecImportant.setEffdate(new Date());
			uecImportant.addOnly();
			Events.postEvent("onOK", this.self, uecImportant);
			this.onCancel();
			Messagebox.show("数据新增成功！");
		}
		if ("edit".equals(opType)) {
			//PubUtil.fillPoFromBean(bean, uecImportant);
			
			//需要修改变量
		//	uecImportant =new UecImportantWork();
			//PubUtil.fillPoFromBean(bean, uecImportant);
			if(!"无".equals(bean.getWorkName().getValue().trim())) {
			uecImportant.setContextName(bean.getContextName().getValue());
			uecImportant.setJoindate(bean.getJoindate().getValue());
			uecImportant.setRoleMakeName((String)bean.getRoleMakeName().getSelectedItem().getValue());
			uecImportant.setRoleProjectName((String)bean.getRoleProjectName().getSelectedItem().getValue());
			uecImportant.setWorkResult(bean.getWorkResult().getValue());
			}
			uecImportant.setWorkName(bean.getWorkName().getValue());
			uecImportant.setJointype((String)bean.getJointype().getSelectedItem().getValue());
			uecImportant.setModifyname(uecStaffAll.getPkpsndoc());
			uecImportant.setStatuscd("1000");
			// 修改时间
			uecImportant.setUpdatedate(new Date());
			uecImportant.updateOnly();
			Events.postEvent("onOK", this.self, uecImportant);
			this.onCancel();
			Messagebox.show("更新成功！");
		}

	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecImportantWorkEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (StrUtil.isNullOrEmpty(bean.getWorkName().getValue().trim())) {
			return "填写项目名称";
		} 
		if(!"无".equals(bean.getWorkName().getValue().trim())) {
		
		  if (StrUtil.isNullOrEmpty(bean.getJoindate().getValue())) {
			return "填写年度日期";
		}else if (StrUtil.isNullOrEmpty(bean.getJointype().getSelectedItem()
				.getValue())) {
			return "填选择参与单位性质";
		}
		  else if (StrUtil.isNullOrEmpty(bean.getRoleProjectName().getSelectedItem()
				.getValue())) {
			return "填写项目角色";
		}
		  else if (StrUtil.isNullOrEmpty(bean.getRoleMakeName().getSelectedItem()
				.getValue())) {
			return "填写制定方案角色";
		}  else if (StrUtil.isNullOrEmpty(bean.getContextName().getValue())) {
			return "请填写工作内容";
		 }    else if (StrUtil.isNullOrEmpty(bean.getWorkResult().getValue())) {
			return "填写项目成果";
		} 
		//字数限制
		 if(getLength(bean.getWorkName().getValue().trim())>80) {
				return "项目名称超出字数限制";
			}
			if(getLength(bean.getContextName().getValue().trim())>400) {
				return "工作内容超出字数限制";
			}
			/*if(getLength(bean.getWorkResult().getValue().trim())>50) {
				return "项目成果超出字数限制";
			}*/
		}else {
			if (StrUtil.isNullOrEmpty(bean.getJointype().getSelectedItem().getValue())) {
				return "填写参与单位性质";
			}
		}
		
		return null;
	}
	public static int getLength(String str) {
		int templ=0;
		templ=str.length();
		return templ;
	}

}
